﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Simple_ToDo.Phone
{
    public partial class MainPage : PhoneApplicationPage
    {
        TaskControl tasks = new TaskControl();
        TaskControl.Categories globalSelectedCategory;

        public MainPage()
        {
            InitializeComponent();

            try
            {
                tasks.DeserializeFromXml();
            }
            catch { }

            listProjects.ItemsSource = tasks.ProjectList;
            listNotes.ItemsSource = tasks.NoteList;

            for (TaskControl.Categories c = TaskControl.Categories.Inbox; c <= TaskControl.Categories.Notes; c++)
            {
                listMain.Items.Add(c.ToString());
            }

            globalSelectedCategory = TaskControl.Categories.Inbox;
        }

        private void btnSelectCategory_Click(object sender, RoutedEventArgs e)
        {
            listTasks.Items.Clear();
            if (((Button)sender).Content.ToString() == TaskControl.Categories.Notes.ToString())
            {
                listMain.Visibility = System.Windows.Visibility.Collapsed;
                listProjects.Visibility = System.Windows.Visibility.Collapsed;
                listNotes.Visibility = System.Windows.Visibility.Visible;
                listTasks.Visibility = System.Windows.Visibility.Collapsed;
                AddTask.Visibility = System.Windows.Visibility.Collapsed;
                EditTask.Visibility = System.Windows.Visibility.Collapsed;
                globalSelectedCategory = TaskControl.Categories.Notes;
            }
            else if (((Button)sender).Content.ToString() == TaskControl.Categories.Projects.ToString())
            {
                listMain.Visibility = System.Windows.Visibility.Collapsed;
                listProjects.Visibility = System.Windows.Visibility.Visible;
                listNotes.Visibility = System.Windows.Visibility.Collapsed;
                listTasks.Visibility = System.Windows.Visibility.Collapsed;
                AddTask.Visibility = System.Windows.Visibility.Collapsed;
                EditTask.Visibility = System.Windows.Visibility.Collapsed;
                globalSelectedCategory = TaskControl.Categories.Projects;
            }
            else
            {
                foreach (Task t in tasks.TaskList)
                    if (t.Category.ToString() == ((Button)sender).Content.ToString())
                        listTasks.Items.Add(t);

                listMain.Visibility = System.Windows.Visibility.Collapsed;
                listProjects.Visibility = System.Windows.Visibility.Collapsed;
                listNotes.Visibility = System.Windows.Visibility.Collapsed;
                listTasks.Visibility = System.Windows.Visibility.Visible;
                AddTask.Visibility = System.Windows.Visibility.Collapsed;
                EditTask.Visibility = System.Windows.Visibility.Collapsed;

                for (TaskControl.Categories c = TaskControl.Categories.Inbox; c <= TaskControl.Categories.Notes; c++)
                {
                    if (((Button)sender).Content.ToString() == c.ToString())
                        globalSelectedCategory = c;
                }
            }
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (listMain.Visibility != System.Windows.Visibility.Visible)
            {
                listMain.Visibility = System.Windows.Visibility.Visible;
                listNotes.Visibility = System.Windows.Visibility.Collapsed;
                listProjects.Visibility = System.Windows.Visibility.Collapsed;
                listTasks.Visibility = System.Windows.Visibility.Collapsed;
                AddTask.Visibility = System.Windows.Visibility.Collapsed;
                EditTask.Visibility = System.Windows.Visibility.Collapsed;
                e.Cancel = true;
            }

            try
            {
                tasks.SerializeToXml();
            }
            catch { }
        }

        private void listTasksCheckBox_Checked(object sender, RoutedEventArgs e)
        {
        }

        private void listProjectsCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            tasks.RemoveProject(((CheckBox)sender).DataContext.ToString());
            listProjects.ItemsSource = null;
            listProjects.Items.Clear();
            listProjects.ItemsSource = tasks.ProjectList;

        }

        private void listNotesCheckBox_Click(object sender, RoutedEventArgs e)
        {
            tasks.RemoveNote(((CheckBox)sender).DataContext.ToString());
            listNotes.ItemsSource = null;
            listNotes.Items.Clear();
            listNotes.ItemsSource = tasks.NoteList;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            listMain.Visibility = System.Windows.Visibility.Collapsed;
            listProjects.Visibility = System.Windows.Visibility.Collapsed;
            listNotes.Visibility = System.Windows.Visibility.Collapsed;
            listTasks.Visibility = System.Windows.Visibility.Collapsed;
            AddTask.Visibility = System.Windows.Visibility.Visible;
            EditTask.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            listMain.Visibility = System.Windows.Visibility.Collapsed;
            listProjects.Visibility = System.Windows.Visibility.Collapsed;
            listNotes.Visibility = System.Windows.Visibility.Collapsed;
            listTasks.Visibility = System.Windows.Visibility.Collapsed;
            AddTask.Visibility = System.Windows.Visibility.Collapsed;
            EditTask.Visibility = System.Windows.Visibility.Visible;

            try
            {
                txtEditNote.Text = ((Task)listTasks.SelectedItem).Note;
                txtEditProject.Text = ((Task)listTasks.SelectedItem).Project;
                txtEditTask.Text = ((Task)listTasks.SelectedItem).Content;
            }
            catch { }

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            tasks.AddTask(txtTask.Text, DateTime.MinValue, DateTime.MinValue, txtProject.Text, globalSelectedCategory, Task.Priorities.nothing, txtNote.Text);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (txtEditTask.Text != "")
            {
                ((Task)listTasks.SelectedItem).Note = txtEditNote.Text;
                ((Task)listTasks.SelectedItem).Project = txtEditProject.Text;
                ((Task)listTasks.SelectedItem).Content = txtEditTask.Text;
            }
        }  
    }
}